var fieldnames ="${allNames}";   //已经填写好的字段数组
layui.use(['form','jquery','layer','table'],function(){
    var form = layui.form,
        $    = layui.jquery,
        layer = layui.layer,
        table = layui.table,
        fieldFormShow,
        ft,
        fieldFormHtml = $("#fieldForm").html();


    $("#openedDiv div i").click(function () { var open_check_value = $("input[name='tabletype']:checked").val()
        console.log(open_check_value); });
    var t = {
        elem: '#demo',
        url:'showFields',
        method:'post',
        width: $(parent.parent.window).width()-233,
        page: false,
        limit:200,
        cols: [[
            {field: 'name', title: '字段名称'},
            {field: 'type', title: '字段类型'},
            {field: 'length', title: '字段长度'},
            {field: 'comment', title: '字段注释'},
            {field: 'listIsShow', title: '是否table显示',templet:'#isShow', align: 'center'},
            {field: 'listIsSearch', title: '是否list搜索',templet:'#isSearch', align: 'center'},
            {field: 'dofor', title: '用途',templet:'#mShow'},
            {field: 'isNullValue', title: '是否可以为空',templet:'#isNullValue'},
            {fixed: 'right',title: '操作', align: 'center',toolbar: '#barDemo'}
        ]],
        where:{s_name:$("#tablenameinput").val(),s_tableType:$("#tabletype").val()}
    };
    var tableload = layer.msg('数据拼命加载中', {
        icon: 16
        ,shade: 0.01
    });
    setTimeout(function(){
        table.render(t);
        layer.close(tableload);
    },500);
    if ($("#tabletype").val() == 3){
        $("#addField").hide();
        $("input[name='length']").val("20").attr("disabled","");
        $("select[name='type']").empty().html('<option value="BIGINT" data-default="20">BIGINT(JAVA:Long)</option>');
        $("select[name='dofor']").empty().html('<option value="0">辅助字段,不在页面显示</option>');
        $("input[name='listIsShow']").removeAttr("checked").attr("disabled","");
        $("input[name='listIsSearch']").removeAttr("checked").attr("disabled","");
        $("input[name='isNullValue']").removeAttr("checked").attr("disabled","");
    }

    $("#addField").on('click',function () {
        $("#fieldForm").empty().html(fieldFormHtml);
        if($("input[name='tabletype']:checked").val() === '3'){
            $("input[name='length']").val("20").attr("disabled","");
            $("select[name='type']").empty().html('<option value="BIGINT" data-default="20">BIGINT(JAVA:Long)</option>');
            $("select[name='dofor']").empty().html('<option value="0">辅助字段,不在页面显示</option>');
            $("input[name='listIsShow']").removeAttr("checked").attr("disabled","");
            $("input[name='listIsSearch']").removeAttr("checked").attr("disabled","");
            $("input[name='isNullValue']").removeAttr("checked").attr("disabled","");
        }
        //重置字段类型
        ft = "";
        //form表单重新渲染
        form.render();
        fieldFormShow =layer.open({
            type: 1,
            title: '添加一个字段',
            area:['700px', '600px'],
            content: $('#fieldForm')
        });
    });

    form.verify({
        typeCheck:function (value,item) {
            if(null == value || "" === value){
                return '字段类型不能为空';
            }
        },
        lengthCheck:function (value,item) {
            if(value !== "" && value != null){
                if(isNaN(value) || value<=0){
                    return '必须输入一个正整数';
                }
            }else{
                var v = $("select[name='type']").find("option:selected").data("default");
                if(v !=null && v !== ""){
                    $("input[name='length']").val(v);
                    return '该类型的字段长度必须大于0';
                }
            }
        },
        fieldname:function (value,item) {
            if(null == value || '' === value){
                return '字段名不能为空';
            }
            if(/[^a-z_]{1,}$/.test(value)){
                return '字段名只能以小写英文开头且只能包含小写英文字母下划线';
            }
            if(/(^\_)|(\__)|(\_+$)/.test(value)){
                return '字段名首尾不能出现下划线\'_\'';
            }
            if(fieldnames.indexOf(value)>=0){
                return '已存在该字段名';
            }
            var checkIndex = layer.load(2, {
                shade: [0.3, '#333']
            });
            $.ajax({
                url:"${base}/admin/system/table/fieldIsExist",
                type:'POST',
                data:{fieldName:value,tableName:'${tableVO.name}'},
                async:false,
                success:function(res){
                    layer.close(checkIndex);
                    if(!res.success){
                        return res.message;
                    }
                }
            });
        },
        updatefieldname:function (value,item) {
            if(null == value || '' === value){
                return '字段名不能为空';
            }
            if(/[^a-z_]{1,}$/.test(value)){
                return '字段名只能以小写英文开头且只能包含小写英文字母下划线';
            }
            if(/(^\_)|(\__)|(\_+$)/.test(value)){
                return '字段名首尾不能出现下划线\'_\'';
            }
            if(updateData.name !== value){
                var checkIndex = layer.load(2, {
                    shade: [0.3, '#333']
                });
                $.ajax({
                    url:"${base}/admin/system/table/fieldIsExist",
                    type:'POST',
                    data:{fieldName:value,tableName:'${tableVO.name}'},
                    async:false,
                    success:function(res){
                        layer.close(checkIndex);
                        if(!res.success){
                            return res.message;
                        }
                    }
                });
            }
        },
        tablename:function (value,item) {
            if(null == value || '' === value){
                return '表名不能为空';
            }
            if(/[^a-z_]{1,}$/.test(value)){
                return '表名只能以小写英文开头且只能包含小写英文字母下划线';
            }
            if(/(^\_)|(\__)|(\_+$)/.test(value)){
                return '字段名首尾不能出现下划线\'_\'';
            }
            if(value.indexOf('sys')>=0){
                return '表名中不能有sys字符';
            }
        }
    });

    form.on("select(typeFilter)",function (data) {
        ft = data.value;
        $("select[name='dofor']").find("option").each(function(){
            $(this).attr("disabled","");
        });
        //清空作用的值
        $("select[name='dofor']").val("");
        //情空作用下面的所有附加的选项
        $("select[name='dofor']").nextAll().remove();
        //先移除length字段的不可用css
        $("input[name='length']").removeAttr("disabled").removeClass("layui-disabled");
        //给长度字段赋值
        $("input[name='length']").val(data.elem.selectedOptions[0].getAttribute("data-default"));
        //如果是时间字段
        if(data.value === "DATE" || data.value === "TIME" || data.value === "DATETIME" || data.value === "TIMESTAMP"){
            $("input[name='length']").attr("disabled","").addClass("layui-disabled");
            $("select[name='dofor']").find("option[value='timer']").removeAttr("disabled");
        }
        //如果是文本字段
        if(data.value === "TEXT"){
            $("input[name='length']").attr("disabled","").addClass("layui-disabled");
            $("select[name='dofor']").find("option[value='editor'],[value='textarea']").removeAttr("disabled");
        }
        //如果是字符串字段
        if(data.value === "VARCHAR" || data.value === "CHAR"){
            $("select[name='dofor']").find("option:not([value=''],[value='timer'],[value='editor'])").removeAttr("disabled");
        }
        //如果是布尔类型的字段
        if(data.value === "BIT"){
            $("select[name='dofor']").find("option[value='switch']").removeAttr("disabled");
        }
        //如果是整形
        if(data.value === "INT" || data.value === "BIGINT" || data.value === "FLOAT" || data.value === "DOUBLE" || data.value === "DECIMAL"){
            $("select[name='dofor']").find("option[value='0'],[value='input'],[value='select'],[value='radio'],[value='switch']").removeAttr("disabled");
        }
        form.render("select");
    });

    form.on("select(doforSelect)",function (data) {
        $("select[name='dofor']").nextAll().remove();
        $("input[name='listIsShow']").removeAttr("disabled");
        $("input[name='listIsSearch']").removeAttr("disabled");

        $("select[name='type']").val(ft);
        $("input[name='length']").val($("select[name='type']").find("option:selected").data("default"));

        if(data.value === "select" || data.value === "radio" || data.value === "checkbox" ){
            $("#doforSelect").append('<a id="addnewkeyvalue"   class="layui-btn layui-btn-normal" style="margin-top: 5px">新增一个KEYVALUE</a>');
            addbutton();
        }

        if(data.value === "switch"){
            $("#doforSelect").append('<input type="checkbox" checked="" value="default" name="defaultValue" id="doswitch" style="width: 100px" lay-filter="doswitch" lay-skin="switch" lay-text="默认|自定">');
            $("select[name='type']").val("BIT");
            $("input[name='length']").val("1");
        }
        if(data.value === '0'){
            $("input[name='listIsShow']").removeAttr("checked").attr("disabled","");
            $("input[name='listIsSearch']").removeAttr("checked").attr("disabled","");
        }
        if(data.value === "uploadImg" || data.value === "uploadFile"){
            $("input[name='listIsSearch']").removeAttr("checked").attr("disabled","");
        }
        form.render();
    });

    form.on('switch(doswitch)', function(data){
        if(data.elem.checked){
            $("#addnewkeyvalue").remove();
            $("#doforSelect").find(".onekeyvalue").remove();
            $("select[name='type']").val("BIT");
            $("input[name='length']").val("1");
        }else{
            $("select[name='type']").val(ft);
            $("input[name='length']").val($("select[name='type']").find("option:selected").data("default"));
            $("#doforSelect").append('<a id="addnewkeyvalue"   class="layui-btn layui-btn-normal" style="margin-top: 5px">新增一个KEYVALUE</a>');
            addbutton();
        }
        form.render("select");
    });

    var addbutton = function () {
        $('#doforSelect').undelegate('a#addnewkeyvalue','click');
        $('#doforSelect').undelegate('.layui-btn-danger','click');
        $('#doforSelect').delegate('a#addnewkeyvalue','click', function() {
            $("#doforSelect").append('<div class="layui-form-item onekeyvalue" style="margin-top: 5px;">\n' +
                '                    <div class="layui-input-inline" style="width: 150px;">\n' +
                '                        <input type="text"   placeholder="KEY" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input key">\n' +
                '                    </div>\n' +
                '                    <div class="layui-form-mid">-</div>\n' +
                '                    <div class="layui-input-inline" style="width: 150px;">\n' +
                '                        <input type="text"   placeholder="VALUE" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input value">\n' +
                '                    </div>\n' +
                '                    <div class="layui-input-inline" style="width: 100px;">\n' +
                '                        <a class="layui-btn layui-btn-danger">删除</a>\n' +
                '                    </div>\n' +
                '                </div>');
            form.render();
        });

        $('#doforSelect').delegate('.layui-btn-danger','click', function() {
            var _this = $(this).parent().parent(),
                dictid = _this.find(".dictid").val();
            if(dictid != null && dictid !== ""){
                $.post("${base}/admin/system/dict/deleteById",{id:dictid},function (res) {
                    if(res.success){
                        _this.remove();
                        // t.width = ($(parent.window).width()-20)*0.8-110;
                        table.reload('demo',t);
                    } else{
                        layer.msg(res.message);
                    }
                });
            }else{
                _this.remove();
            }
            form.render();
        });

    };
    //字段新增
    form.on("submit(fieldFilter)",function (data) {
        var flag = true;
        //处理开关数据
        if(undefined === data.field.isNullValue || null == data.field.isNullValue){
            data.field.isNullValue = 'NO';
        }else{
            data.field.isNullValue = 'YES';
        }
        if(undefined === data.field.listIsShow || null == data.field.listIsShow){
            data.field.listIsShow = false
        }else{
            data.field.listIsShow = true
        }

        if(undefined === data.field.listIsSearch || null == data.field.listIsSearch){
            data.field.listIsSearch = false
        }else{
            data.field.listIsSearch = true
        }
        //处理多选项数据
        if(data.field.dofor === "select" || data.field.dofor === "radio" || data.field.dofor === "checkbox" || data.field.dofor === "switch" ){
            var dictlist = [],    //字典集合
                templabel = [];   //临时存放已经写好的key;
            $("#doforSelect").find(".onekeyvalue").each(function (index,element) {
                var dict = {},
                    label = $(this).find(".key").val(),
                    value = $(this).find(".value").val();
                if(templabel.indexOf(label)>=0){
                    layer.msg("有相同名称的label值");
                    flag =false;
                }else {
                    dict.type = "${tableVO.name}_"+data.field.name;
                    dict.label = label;
                    dict.value = value;
                    dict.sort = index;
                    templabel.push(label);
                    dictlist.push(dict);
                }
            });
            if(flag && data.field.dofor !== "switch"){
                if(dictlist.length === 0){
                    layer.msg("选项的预置KEY-VALUE不能为空");
                    flag = false;
                }
                data.field.defaultValue = false;
            }
            if(flag && data.field.dofor === "switch"){
                if(undefined === data.field.defaultValue || false === data.field.defaultValue || null == data.field.defaultValue) {
                    if (dictlist.length !== 2) {
                        layer.msg("自定义开关选项的预置KEY-VALUE只能为两项");
                        flag = false;
                    }else{
                        if(dictlist[0].value === dictlist[1].value){
                            layer.msg("自定义开关选项的预置KEY-VALUE值不能相同");
                            flag = false;
                        }else{
                            data.field.defaultValue = false;
                        }
                    }
                }else{
                    data.field.defaultValue = true;
                }
            }
        }
        if(flag){
            data.field.dictlist = dictlist;
            var loadIndex = layer.load(2, {
                shade: [0.3, '#333']
            });
            //填充表格数据
            $.ajax({
                type:"POST",
                url:"${base}/admin/system/table/addField",
                dataType:"json",
                contentType:"application/json",
                data:JSON.stringify(data.field),
                success:function(res){
                    layer.close(loadIndex);
                    if(res.success){
                        layer.msg("字段新增成功",{time:1000},function () {
                            location.reload();
                        });
                    }else{
                        layer.msg(res.message,{time:1000});
                    }
                }
            });
            //关闭新建表单窗口
            layer.close(fieldFormShow);
        }
        return false;
    });

    //字段修改
    form.on("submit(editfield)",function (data) {
        var flag = true;
        //处理开关数据
        if(undefined === data.field.isNullValue || null == data.field.isNullValue){
            data.field.isNullValue = 'NO';
        }else{
            data.field.isNullValue = 'YES';
        }
        if(undefined === data.field.listIsShow || null == data.field.listIsShow){
            data.field.listIsShow = false
        }else{
            data.field.listIsShow = true
        }

        if(undefined === data.field.listIsSearch || null == data.field.listIsSearch){
            data.field.listIsSearch = false
        }else{
            data.field.listIsSearch = true
        }
        //处理多选项数据
        if(data.field.dofor === "select" || data.field.dofor === "radio" || data.field.dofor === "checkbox" || data.field.dofor === "switch" ){
            var dictlist = [],    //字典集合
                templabel = [];   //临时存放已经写好的key;
            $("#doforSelect").find(".onekeyvalue").each(function (index,element) {
                var dict = {},
                    label = $(this).find(".key").val(),
                    value = $(this).find(".value").val();
                if(templabel.indexOf(label)>=0){
                    layer.msg("有相同名称的label值");
                    flag =false;
                }else {
                    dict.type =$("#tablenameinput").val()+"_"+data.field.name;
                    dict.label = label;
                    dict.value = value;
                    dict.sort = index;
                    templabel.push(label);
                    dictlist.push(dict);
                }
            });
            if(flag && data.field.dofor !== "switch"){
                if(dictlist.length === 0){
                    layer.msg("选项的预置KEY-VALUE不能为空");
                    flag = false;
                }
                data.field.defaultValue = false;
            }
            if(flag && data.field.dofor === "switch"){
                if(undefined === data.field.defaultValue || false === data.field.defaultValue || null == data.field.defaultValue) {
                    if (dictlist.length !== 2) {
                        layer.msg("自定义开关选项的预置KEY-VALUE只能为两项");
                        flag = false;
                    }else{
                        if(dictlist[0].value === dictlist[1].value){
                            layer.msg("自定义开关选项的预置KEY-VALUE值不能相同");
                            flag = false;
                        }else{
                            data.field.defaultValue = false;
                        }
                    }
                }else{
                    data.field.defaultValue = true;
                }
            }
        }
        if(flag){
            data.field.dictlist = dictlist;
            var loadIndex = layer.load(2, {
                shade: [0.3, '#333']
            });
            //填充表格数据
            $.ajax({
                type:"POST",
                url:"editField",
                dataType:"json",
                contentType:"application/json",
                data:JSON.stringify(data.field),
                success:function(res){
                    layer.close(loadIndex);
                    if(res.success){
                        layer.msg("字段编辑成功",{time:1000},function () {
                            location.reload();
                        });
                    }else{
                        layer.msg(res.message,{time:1000});
                    }
                }
            });
            //关闭新建表单窗口
            layer.close(fieldFormShow);
        }
        return false;
    });

    var updateId,updateData;
    //监听工具条
    table.on('tool(demo)', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){
            layer.confirm('真的删除行么', function(index){
                obj.del();
                //删除表格数据
                $.post("${base}/admin/system/table/deleteField",{fieldName:data.name,tableName:'${tableVO.name}'},function(res){
                    if(res.success){
                        layer.msg("删除成功",{time:1000},function () {
                            location.reload();
                        });
                    }else{
                        layer.msg(res.message);
                    }
                });
                layer.close(index);
            });
        } else if(obj.event === 'edit'){
            // layer.alert('编辑行：<br>'+ JSON.stringify(data));
            $("select[name='dofor']").nextAll().remove();
            $("#myform")[0].reset();
            updateId = obj.tr[0].rowIndex;
            updateData = data;
            $($("#myform")[0]).find('input,select').each(function () {
                var name = $(this).attr('name');
                if ("" === name || undefined === name) return;
                if(name === "name" || name === 'comment'){
                    $(this).val(data[name]);
                    return;
                }
                if(name === 'oldName'){
                    $(this).val(data['name']);
                    return;
                }
                if(name === 'type'){
                    $("select[name='dofor']").find("option").each(function () {
                        $(this).attr("disabled","");
                    });
                    var v = data['type'];
                    ft = v;
                    $(this).val(v);
                    //如果是时间字段
                    if(v === "DATE" || v === "TIME" || v === "DATETIME" || v === "TIMESTAMP"){
                        $("input[name='length']").attr("disabled","").addClass("layui-disabled");
                        $("select[name='dofor']").find("option[value='timer']").removeAttr("disabled");
                    }
                    //如果是文本字段
                    if(v === "TEXT"){
                        $("input[name='length']").attr("disabled","").addClass("layui-disabled");
                        $("select[name='dofor']").find("option[value='editor'],[value='textarea']").removeAttr("disabled");
                    }
                    //如果是字符串字段
                    if(v === "VARCHAR" || v === "CHAR"){
                        $("select[name='dofor']").find("option:not([value=''],[value='timer'],[value='editor'])").removeAttr("disabled");
                    }
                    //如果是布尔类型的字段
                    if(v === "BIT"){
                        $("select[name='dofor']").find("option[value='switch']").removeAttr("disabled");
                    }
                    //如果是整形
                    if(v === "INT" || v === "BIGINT" || v === "FLOAT" || v === "DOUBLE" || v === "DECIMAL"){
                        $("select[name='dofor']").find("option[value='0'],[value='input'],[value='select'],[value='radio'],[value='switch']").removeAttr("disabled");
                    }

                    return;
                }
                if(name === "length"){
                    $(this).val(data[name]);
                    if(data['type'] === "DATE" || data['type'] === "TIME" || data['type'] === "DATETIME" || data['type'] === "TIMESTAMP" || data['type'] === "TEXT"){
                        $(this).attr("disabled","").addClass("layui-disabled");
                    }else{
                        $(this).removeAttr("disabled").removeClass("layui-disabled");
                    }
                    return;
                }
                if(name === "listIsShow" || name === "listIsSearch"){
                    if(data[name] === false){
                        $(this).removeAttr("checked");
                    }else{
                        $(this).attr("checked","");
                    }
                    return;
                }
                if(name === 'dofor'){
                    $(this).val(data['dofor']);
                    if(data['dofor'] === "select" || data['dofor'] === "radio" || data['dofor'] === "checkbox" || data['dofor'] === "switch"){
                        var dictlist = data.dictlist;
                        var mdefault = data.defaultValue;
                        if(data['dofor'] === "switch"){
                            if(mdefault !== undefined  && mdefault != null && mdefault === true){
                                $("#doforSelect").append('<input type="checkbox" checked="" value="default" name="defaultValue" id="doswitch" style="width: 100px" lay-filter="doswitch" lay-skin="switch" lay-text="默认|自定">');
                            }else{
                                $("#doforSelect").append('<input type="checkbox"  value="default" name="defaultValue" id="doswitch" style="width: 100px" lay-filter="doswitch" lay-skin="switch" lay-text="默认|自定">');
                            }
                        }
                        if(mdefault === undefined || mdefault === false || mdefault == null){
                            $("#doforSelect").append('<a id="addnewkeyvalue"   class="layui-btn layui-btn-normal" style="margin-top: 5px">新增一个KEYVALUE</a>');
                        }
                        if(dictlist !== undefined && dictlist != null && dictlist.length>0){
                            for(var i = 0;i<dictlist.length;i++){
                                $("#doforSelect").append('<div class="layui-form-item onekeyvalue" style="margin-top: 5px;">\n' +
                                    '                    <div class="layui-input-inline" style="width: 150px;">\n' +
                                    '                        <input type="hidden" class="dictid" value="'+dictlist[i].id+'">\n' +
                                    '                        <input type="text" value="'+dictlist[i].label+'"   placeholder="KEY" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input key">\n' +
                                    '                    </div>\n' +
                                    '                    <div class="layui-form-mid">-</div>\n' +
                                    '                    <div class="layui-input-inline" style="width: 150px;">\n' +
                                    '                        <input type="text" value="'+dictlist[i].value+'"   placeholder="VALUE" lay-verify="required" lay-verType="tips" autocomplete="off" class="layui-input value">\n' +
                                    '                    </div>\n' +
                                    '                    <div class="layui-input-inline" style="width: 100px;">\n' +
                                    '                        <a class="layui-btn layui-btn-danger">删除</a>\n' +
                                    '                    </div>\n' +
                                    '                </div>');
                            }
                        }
                    }
                    if(data['dofor'] === '0'){
                        $("input[name='listIsShow']").removeAttr("checked").attr("disabled","");
                        $("input[name='listIsSearch']").removeAttr("checked").attr("disabled","");
                    }else{
                        $("input[name='listIsShow']").removeAttr("disabled");
                        $("input[name='listIsSearch']").removeAttr("disabled");
                    }
                    return;
                }
                if(name === "isNullValue" ){
                    if(data['isNullValue'] === 'NO'){
                        $(this).removeAttr("checked");
                    }else{
                        $(this).attr("checked","");
                    }

                }
            });
            $("#myname").attr("lay-verify","updatefieldname");
            $("#fieldsubmit").attr("lay-filter","editfield");
            form.render();
            fieldFormShow =layer.open({
                type: 1,
                title: '修改一个字段',
                area:['700px', '600px'],
                content: $('#fieldForm')
            });
        }
    });

    form.on("submit(addUser)",function(data){
        var loadIndex = layer.load(2, {
            shade: [0.3, '#333']
        });
        $.post('${base}/admin/system/table/editTable',data.field,function (res) {
            layer.close(loadIndex);
            if(res.success){
                parent.layer.msg("数据表更新成功!",{time:1500},function(){
                    //刷新父页面
                    parent.location.reload();
                });
            }else{
                layer.msg(res.message);
            }
        });
        return false;
    });

});